//
//  MSSearchBar.m
//  MS
//
//  Created by Yuan on 13-4-6.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSSearchBar.h"
#import "UIColor+ColorUtils.h"

@implementation MSSearchBar

@synthesize delegate,text;

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:CGRectMake(frame.origin.x,
										   frame.origin.y,
										   frame.size.width,
										   44.0)];
    if (self) {

        self.backgroundColor = [UIColor colorWithString:@"#dcdbdb"];
		
		txtField=[[UITextField alloc]initWithFrame:CGRectMake(10.0f,8.0f,
															  frame.size.width-90.0f,31)];
		txtField.borderStyle=UITextBorderStyleRoundedRect;
		txtField.clearButtonMode=UITextFieldViewModeWhileEditing;
		txtField.delegate=self;
        txtField.font = FONT_NORMAL;
        txtField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
		txtField.returnKeyType=UIReturnKeyDone;
        txtField.placeholder = HYLocalizedString(@"请输入名称关键字");
		[self addSubview:txtField];
		
		NSString *imgPath=[[NSBundle mainBundle]pathForResource:@"btn_search" ofType:@"png"];
		UIImage *theBtnImg=[UIImage imageWithContentsOfFile:imgPath];
		searchBtn=[UIButton buttonWithType:UIButtonTypeCustom];
		[searchBtn setBackgroundImage:theBtnImg forState:UIControlStateNormal];
		searchBtn.titleLabel.font= FONT_NORMAL;
		[searchBtn addTarget:self action:@selector(searchBtnDone:) forControlEvents:UIControlEventTouchUpInside];
		searchBtn.frame=CGRectMake(txtField.frame.origin.x+txtField.frame.size.width+11.0f,
								   txtField.frame.origin.y,60.0f, 31.0f);
		[self addSubview:searchBtn];
		
		self.text=txtField.text;
    }
    return self;
}


-(void)searchBtnDone:(id)sender
{
	UIButton *btn=(UIButton *)sender;
	NSString *searchText=txtField.text;
	
	if (delegate&&[delegate respondsToSelector:@selector(searchToolSearchBtnDone:searchToolView:searchText:)]) {
		[delegate searchToolSearchBtnDone:btn searchToolView:self searchText:searchText];
	}
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
	if (delegate&&[delegate respondsToSelector:@selector(searchToolDidBegainEditing:)]) {
		[delegate searchToolDidBegainEditing:self];
	}
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
	if (delegate&&[delegate respondsToSelector:@selector(searchToolDidEndEditing:)]) {
		[delegate searchToolDidEndEditing:self];
	}
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
	if (delegate&&[delegate respondsToSelector:@selector(searchToolShoulReturn:)]) {
		[delegate searchToolShoulReturn:self];
	}
	return YES;
}

-(BOOL)resignFirstResponder
{
	return [txtField resignFirstResponder];
}

-(BOOL)becomeFirstResponder
{
	return [txtField becomeFirstResponder];
}

-(void)setText:(NSString *)_txt
{
	txtField.text=_txt;
	text= txtField.text;
}

-(NSString *)text
{
	return txtField.text;
}

- (void)dealloc {
	delegate=nil;
}


@end
